#include "gpu_utility.h"
#include <DXGI.h>
#include <vector>
#include <stdio.h>
#include <string.h>

/**
 * @brief Converts a wide string to a standard string
 * @param wstr Wide string to be converted
 * @return Converted standard string
 */
std::string WStringToString(const std::wstring& wstr)
{
	std::string str(wstr.length(), ' ');
	std::copy(wstr.begin(), wstr.end(), str.begin());
	return str;
}

/**
 * @brief Gets the current GPU model name using DXGI interface
 * @return GPU model name as string, empty string if failed
 */
std::string GetGPUModel()
{
	IDXGIFactory* pFactory;
	IDXGIAdapter* pAdapter;
	std::vector <IDXGIAdapter*> vAdapters;
	int iAdapterNum = 0;

	HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory));

	if (FAILED(hr))
		return "";

	while (pFactory->EnumAdapters(iAdapterNum, &pAdapter) != DXGI_ERROR_NOT_FOUND)
	{
		vAdapters.push_back(pAdapter);
		++iAdapterNum;
	}

	for (size_t i = 0; i < vAdapters.size(); i++)
	{
		DXGI_ADAPTER_DESC adapterDesc;
		vAdapters[i]->GetDesc(&adapterDesc);
		std::wstring wad(adapterDesc.Description);
		std::string wa = WStringToString(wad);
		return wa;
	}
	vAdapters.clear();

	return "";
}